"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox的key_value参数，连接符和分隔符异常验证
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、修改元数据指定指定多个有效key值，分隔符以非，形式
    3、修改元数据指定根叶类型，连接符以非.形式
Expect      :
    1、查看成功
    2、修改失败
    3、修改失败
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.core_ctrl_meta = ['version', 'volume_count', 'au_size']
        self.rf_block_meta = 'ft_root-ft_root_free_list-last'
        self.core_ctrl_meta_value = [3707, 2, 8192]

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        text = '-----step2:修改元数据指定指定多个有效key值，分隔符以非，形式;  expect:修改失败-----'
        self.log.info(text)

        key_value = ''
        for param in self.core_ctrl_meta:
            key_value += f'{param}={self.core_ctrl_meta_value[self.core_ctrl_meta.index(param)]};'
            self.log.info(key_value)

        cmd1 = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -v {vol_value} -t core_ctrl -k '{key_value}'"
        self.log.info(cmd1)
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
        self.log.info(execute_cmd1)
        res1 = self.user_node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Invalid intput key_value', res1, '修改元数据成功')

        text = '-----step3:修改元数据指定根叶类型，连接符以非.形式;  expect:修改失败-----'
        self.log.info(text)
        cmd2 = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -v {vol_value} -t root_ft_block -k '{self.rf_block_meta}=1000'"
        self.log.info(cmd2)
        self.user_node.sh(cmd2).result()
        res2 = self.user_node.sh(cmd2).result()
        self.log.info(res2)
        self.assertIn('Invalid intput key_value', res2, '修改元数据成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
